package com.te;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Element {

    int    x, y;
    Bitmap mBitmap;

    public Element(Resources res, int x, int y) {
        mBitmap = BitmapFactory.decodeResource(res, R.drawable.icon);

        this.x = x - mBitmap.getWidth() / 2;
        this.y = y - mBitmap.getHeight() / 2;
    }

    public void doDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmap, x, y, null);
        // Log.d("el","xy "+x+":"+y);
    }

    public void animate(long elapsedTime) {
        x += 1; // * (elapsedTime / 20f);
        y += 2; // * (elapsedTime / 20f);
        checkBorders();
    }

    private void checkBorders() {
        if (x <= 0) {

            x = 0;
        }
        else if (x + mBitmap.getWidth() >= GameWorld.width) {

            x = GameWorld.width - mBitmap.getWidth();
        }
        if (x <= 0) {
            y = 0;
        }
        if (y + mBitmap.getHeight() >= GameWorld.height) {

            y = GameWorld.height - mBitmap.getHeight();
        }
    }

    public int getCenterX() {
        return x + mBitmap.getWidth() / 2;
    }

    public int getCenterY() {
        return y + mBitmap.getHeight() / 2;
    }

}
